package com.google.code.smon;

import com.google.code.smon.util.CustomFormatter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 *
 * @author Simon Beckstein <simon.beckstein at gmail.com>
 */
public class LogSetup {

    public static final Logger rootLog = Logger.getLogger("");
    public static final String propperties = "/config/logger.propperties";
    public static boolean enableDebugEventHandler = false;
    private static final Logger classLog = Logger.getLogger(LogSetup.class.getName());
//    private static final EventDispatcher disp = EventDispatcher.getInstance();

    public static void setup() {
//        disp.addGlobalListener(new EventListener() {
//
//            public void handleEvent(Event e) {
//                if (enableDebugEventHandler) {
//                    classLog.logp(Level.FINEST, EventDispatcher.class.getName(), e.getName(), e.getName() + " triggered", e.getContext());
//                }
//            }
//        });

        LogManager.getLogManager().reset();
        rootLog.setLevel(Level.CONFIG);
        Handler console = new ConsoleHandler();
        console.setFormatter(new CustomFormatter("%t [%L] %m [%C.class:%M]", "HH:mm:ss"));
        console.setLevel(Level.ALL);
        rootLog.addHandler(console);
        Logger.getLogger("com.google.code.smon").setLevel(Level.ALL);


        try {
            InputStream is = LogSetup.class.getClassLoader().getResourceAsStream(propperties);
            if (is == null) {
                throw new FileNotFoundException("could not find logger.propperties: " + propperties);
            }
            LogManager.getLogManager().readConfiguration();
        } catch (IOException ex) {
            rootLog.log(Level.SEVERE, "Cant find default logger configuration", ex);

        } catch (SecurityException ex) {
            rootLog.log(Level.SEVERE, "Not allowed to read default logger configuration", ex);
        }

        //Handler windowLog = WindowLogHandler.getInstance();
        //windowLog.setFormatter(new CustomFormatter("[%t] %L: %m", "HH:mm:ss"));

        //rootLog.addHandler(windowLog);
        rootLog.info("Logger Inititalized");
        String targets = "";
        for (Handler handler : rootLog.getHandlers()) {
            targets += handler.getClass().getSimpleName() + "(" + handler.getLevel() + ") ";
        }
        rootLog.log(Level.CONFIG, "logging initialized", targets);
        rootLog.fine("Installing UncaughtExceptionHandler");
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                rootLog.log(Level.SEVERE, "Uncaught " + e.getClass().
                        getSimpleName() + " in " + t.getName(), e);
                e.printStackTrace();
//                disp.triggerEvent("onUncaughtException", true, t, e);
            }
        });
    }

    public static void testLog() {
        classLog.finest("finest message");
        classLog.finer("finer message");
        classLog.fine("fine message");
        classLog.config("config message");
        classLog.info("info message");
        classLog.warning("warning message");
        classLog.severe("severe message");

        classLog.log(Level.INFO, "message with 1 param", "this is 1 param");
        classLog.log(Level.INFO, "mesage with param array", rootLog.getHandlers());
        classLog.log(Level.SEVERE, "message with throwable", new Throwable("test"));
    }
}
